/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fork;

import tal.drivechain.chain.Chain;
import tal.drivechain.chain.ChainListener;
import tal.drivechain.chain.ChainNode;
import tal.drivechain.plugin.Channel;
import tal.drivechain.plugin.Module;
import tal.drivechain.system.Globals;
import tal.drivechain.variable.Variable;

/**
 *
 * @author Tal Eisenberg
 */
public class ChainEvents extends Module implements ChainListener {
    private Chain chain = null;

    public void setChain(String chainPath) {
        if (chainPath==null) {
            if (chain!=null) {
                if (isRunning()) chain.removeChainListener(this);
                chain = null;
            }
        }
        else {
            Chain c = Globals.findChain(chainPath, this.getParentChain());
            if (chain!=null && isRunning()) chain.removeChainListener(this);
            chain = c;
            if (isRunning()) chain.addChainListener(this);
        }
    } public String getChain() {
        if (chain==null) return null;
        else return chain.getPath();
    }

    @Override
    public void start() {
        super.start();
        if (chain!=null)
            chain.addChainListener(this);
    }

    @Override
    public void stop() {
        if (chain!=null)
            chain.removeChainListener(this);
        super.stop();
    }


    @Override
    protected void msgReceived(Variable in, long timestamp, Channel channel) {
    }

    public void chainStopped(Chain src) {
    }

    public boolean chainStopping(Chain src) {
        return true;
    }

    public boolean chainStarting(Chain src) {
        return true;
    }

    public void nodeConfigured(Chain src, ChainNode child) {
    }

    public void childRemoved(Chain src, ChainNode child) {
    }

    public void childAdded(Chain src, ChainNode child) {
    }

    public void childRenamed(Chain src, ChainNode child) {
    }
}
